<template>
  <div>
    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中"
      :data="videoList"
      border
      style="width: 100%"
    >
      <el-table-column fixed prop="title" label="标题" width="250"></el-table-column>
      <el-table-column prop="img" label="封面" width="150">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.img | toCoverUrl"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="show" label="公开" width="100">
        <template slot-scope="scope">{{ scope.row.show | isShow }}</template>
      </el-table-column>
      <el-table-column prop="content" label="简介" width="250"></el-table-column>
      <el-table-column prop="date" label="日期" width="220">
        <template slot-scope="scope">{{ scope.row.date | toTime }}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template slot-scope="scope">
          <el-button @click="modifyRow(scope.row)" type="text" size="small">修改</el-button>
          <el-button @click="deleteVideo(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="修改视频信息" :visible.sync="dialogFormVisible">
      <el-form :model="modifyForm" :rules="rules" ref="modifyForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="modifyForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" prop="content">
          <el-input v-model="modifyForm.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="可见">
          <el-switch v-model="modifyForm.isShow"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="beforemodifyVideo('modifyForm')">确 定</el-button>
      </div>
    </el-dialog>
    <el-pagination
      background
      layout="prev, pager, next"
      :current-page="page"
      :total="count"
      :page-size="page_size"
      @current-change="handleCurrent"
    ></el-pagination>
  </div>
</template>

<script>
import { getVideoList, modifyVideo, deleteVideo } from "@/api/adminSerivce";
import { coverUrl } from "@/utils/url";
export default {
  data() {
    return {
      dialogFormVisible: false,
      loading: true,
      videoList: [],
      page: 1,
      page_size: 10,
      count: 0,
      formId: {
        id: "",
      },
      modifyForm: {
        id: "",
        title: "",
        content: "",
        isShow: true,
      },
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入简介", trigger: "blur" }],
      },
    };
  },
  methods: {
    _getVideoList() {
      getVideoList(this.page, this.page_size).then((res) => {
        if (res.data.code === 200) {
          if (this.count == 0) {
            //只有查询第一页才返回总数
            this.count = res.data.data.count;
          }
          this.videoList = res.data.data.data;
          this.loading = false;
        }
      });
    },
    handleCurrent(page) {
      this.page = page;
      this._getVideoList();
    },
    modifyRow(row) {
      this.modifyForm.id = row.vid;
      this.modifyForm.title = row.title;
      this.modifyForm.content = row.content;
      this.modifyForm.isShow = row.show;
      this.dialogFormVisible = true;
    },
    beforemodifyVideo(userForm) {
      this.$refs[userForm].validate((valid) => {
        if (valid) {
          this.modifyVideo();
        } else {
          this.$message.error("请检查输入的数据");
          return;
        }
      });
    },
    modifyVideo() {
      modifyVideo(this.modifyForm).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("修改成功");
          this.dialogFormVisible = false;
          this._getVideoList();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    deleteVideo(row) {
      this.formId.id = row.vid;
      deleteVideo(this.formId).then((res) => {
        if (res.data.code === 200) {
          this.$message.success("删除成功");
          //重新获取
          this._getVideoList();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
  },
  created() {
    this._getVideoList();
  },
  filters: {
    isShow(show) {
      if (show) {
        return "公开";
      } else {
        return "隐藏";
      }
    },
    toCoverUrl(name) {
      return coverUrl(name);
    },
    toTime(time) {
      var date = new Date(time);
      var year = date.getFullYear();
      var month =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      var hours =
        date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      var minutes =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      return year + "-" + month + "-" + day + "  " + hours + ":" + minutes;
    },
  },
};
</script>
